package com.sanpu.kxe.business.main.view;

import android.app.Activity;
import android.graphics.Bitmap;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.view.View;
import android.widget.ListView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.animation.GlideAnimation;
import com.bumptech.glide.request.target.SimpleTarget;
import com.sanpu.kxe.R;
import com.sanpu.kxe.base.BaseTitleActivity;
import com.sanpu.kxe.business.main.adapter.PushDetailAdapter;
import com.sanpu.kxe.business.main.bean.PushDetailsBean;
import com.sanpu.kxe.business.main.contract.MainConfig;
import com.sanpu.kxe.common.interfaces.UrlConstants;
import com.sanpu.kxe.utils.ScreenUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;

/**
 * Description：地推物料详情
 * Created by 薛勇军 on 2018/8/23.
 */

public class PushDetailActivity extends BaseTitleActivity {

    @Bind(R.id.pushLv)
    ListView vListView;

    private PushDetailAdapter mAdapter;

    private String[] strs = null;

    private List<PushDetailsBean> dataList = new ArrayList<>();

    private int count = 0;

    @Override
    protected int getLayoutResID() {
        return R.layout.activity_push_detail;
    }

    @Override
    protected void initView() {
        super.initView();
        String title = getIntent().getStringExtra(MainConfig.INTENT_MATERIAL_NAME);
        if (!TextUtils.isEmpty(title)) {
            commonTitle.setTitle(title);
        }
        View footerView = View.inflate(mContext, R.layout.view_push_footer, null);
        vListView.addFooterView(footerView);
        mAdapter = new PushDetailAdapter(mContext, new ArrayList<PushDetailsBean>());
        vListView.setAdapter(mAdapter);
    }

    @Override
    protected void initData() {
        super.initData();
        String posterPath = getIntent().getStringExtra(MainConfig.INTENT_MATERIAL_PATH);
        if (!TextUtils.isEmpty(posterPath)) {
            strs = posterPath.split(",");
        }
        if (strs != null && strs.length>0) {
//            showLoadingDialog(false, "正在加载中...");
            onLoadImage();
        }
    }

    /**
     * 设置图片的宽高
     */
    private void onLoadImage() {
        Glide.with(mContext)
                .load(UrlConstants.getBaseUrl()+strs[count])//强制Glide返回一个Bitmap对象
                .asBitmap()
                .into(new SimpleTarget<Bitmap>() {
                    @Override
                    public void onResourceReady(Bitmap bitmap, GlideAnimation<? super Bitmap> glideAnimation) {
                        int width = bitmap.getWidth();
                        int height = bitmap.getHeight();
                        DisplayMetrics metric = new DisplayMetrics();
                        ((Activity)mContext).getWindowManager().getDefaultDisplay().getMetrics(metric);
                        int pxWidth = metric.widthPixels- ScreenUtil.dp2px(30);
                        float ratio = (float)pxWidth/width;
                        float imageHeight = height*ratio;

                        PushDetailsBean pushDetailBean = new PushDetailsBean();
                        pushDetailBean.setUrl(UrlConstants.getBaseUrl()+strs[count]);
                        pushDetailBean.setWidth(pxWidth);
                        pushDetailBean.setHeight(imageHeight);
                        dataList.add(pushDetailBean);

                        count++;
                        if (count == strs.length) {
//                            dismissLoadingDialog();
                            mAdapter.setNotifyData(dataList);
                        } else {
                            onLoadImage();
                        }
                    }
                });
    }

    @Override
    protected boolean hasBindEventBus() {
        return false;
    }
}
